﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace L4View.PresentationLayer
{
    using BusinessModel;
    using BusinessModel.SpyLogMonitor;

    public class SpyLogPresenter
    {
        private ILoggingMonitor _logMonitor;
        
        private ISpyLogView _view;

        private Pager<ILogMessage> _pager;

        public SpyLogPresenter(ILoggingMonitor logMonitor, ISpyLogView view )
        {
            _logMonitor = logMonitor;
            _view = view;
            _pager = new Pager<ILogMessage>(logMonitor.LogCollection.LogMessages, 10);

            SubscribeEvents();
            SetupFilters();
        }

        private void SubscribeEvents()
        {
            _view.StartMonitoring += OnStartMonitoring;
            _view.StopMonitoring += OnStopMonitoring;
            _view.Command += OnCommand;

            _logMonitor.LogCollection.IncomingMessages += OnIncomingMessages;
        }

        private void SetupFilters()
        {
            _view.PageSizes = new List<ListItem>() 
                { 
                    new ListItem( "10", 10 ),
                    new ListItem( "20", 20 ),
                    new ListItem( "30", 30 ),
                    new ListItem( "50", 50 ),
                    new ListItem( "100", 100 ),
                    new ListItem( "All", 0 )
                };

            _pager.PageSize = 10;
            
            _view.CurrentPageNumber = _pager.PageCount * _pager.PageSize - 1;
            _view.PageSize = _pager.PageSize;
        }

        private void OnCommand(object sender, SpyLogViewEventArgs e)
        {
            switch (e.Command)
            {
                case SpyLogViewCommands.FirstPage:
                    _pager.First();
                    break;

                case SpyLogViewCommands.LastPage:
                    _pager.Last();
                    break;

                case SpyLogViewCommands.PrevPage:
                    _pager.Prev();
                    break;

                case SpyLogViewCommands.NextPage:
                    _pager.Next();
                    break;

                case SpyLogViewCommands.MoveToPage:
                    _pager.PageNumber = Convert.ToInt32(e.Argument);
                    break;

                case SpyLogViewCommands.ChangePageSize:
                    _pager.PageSize = Convert.ToInt32(e.Argument);
                    break;

                case SpyLogViewCommands.SortBy:
                    break;

                default:
                    break;
            }

            DisplayMessages();
        }

        private void OnStartMonitoring(object sender, EventArgs e)
        {
            _logMonitor.Start(true);
        }

        private void OnStopMonitoring(object sender, EventArgs e)
        {
            _logMonitor.Stop();
        }

        private void OnIncomingMessages()
        {
            DisplayMessages();
        }

        private void DisplayMessages()
        {
            _view.Messages = _logMonitor.LogCollection.LogMessages
                .Page(_pager.PageNumber, _pager.PageSize);

            _view.PageCount = _pager.PageCount;
        }
    }
}
